package com.gustuy.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ChineseEncoding implements Filter {

    //初始化
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    //过滤
    /*
    1.过滤器中的所有代码，在过滤特定请求时都会执行
    2.必须让过滤器继续通行 chain.doFilter(request,response);
    特点请求在web.xml配置
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        HttpServletResponse resp = (HttpServletResponse) response;

        req.setCharacterEncoding("utf-8");

        resp.setContentType("text/html; charset=UTF-8");

        resp.setCharacterEncoding("utf-8");
        chain.doFilter(req,resp);
    }

    //销毁
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
